home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / bin / zegrep < prev    next >
Encoding:
Text File  |  2007-01-16  |  4.0 KB  |  155 lines

  1. #!/bin/sh
  2.  
  3. # zgrep -- a wrapper around a grep program that decompresses files as needed
  4. # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
  5.  
  6. # Copyright (C) 1998, 2001, 2002, 2006 Free Software Foundation
  7. # Copyright (C) 1993 Jean-loup Gailly
  8.  
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22.  
  23. PATH="/usr/bin:$PATH"; export PATH
  24.  
  25. prog=`echo "$0" | sed 's|.*/||'`
  26. case "$prog" in
  27.     *egrep)    grep=${EGREP-egrep}    ;;
  28.     *fgrep)    grep=${FGREP-fgrep}    ;;
  29.     *)    grep=${GREP-grep}    ;;
  30. esac
  31.  
  32. version="z$grep (gzip) 1.3.9
  33. Copyright (C) 2006 Free Software Foundation, Inc.
  34. This is free software.  You may redistribute copies of it under the terms of
  35. the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
  36. There is NO WARRANTY, to the extent permitted by law.
  37.  
  38. Written by Jean-loup Gailly."
  39.  
  40. usage="Usage: z$grep [OPTION]... [-e] PATTERN [FILE]...
  41. Look for instances of PATTERN in the input FILEs, using their
  42. uncompressed contents if they are compressed.
  43.  
  44. OPTIONs are the same as for '$grep'.
  45.  
  46. Report bugs to <bug-gzip@gnu.org>."
  47.  
  48. pat=""
  49. after_dash_dash=""
  50. files_with_matches=0
  51. files_without_matches=0
  52. no_filename=0
  53. with_filename=0
  54.  
  55. while test $# -ne 0; do
  56.   case "$after_dash_dash$1" in
  57.   --d* | --rec*)    echo >&2 "$0: $1: option not supported"; exit 2;;
  58.   --h*)            echo "$usage" || exit 2; exit;;
  59.   --files-with-*)    files_with_matches=1;;
  60.   --files-witho*)    files_without_matches=1;;
  61.   --no-f*)    no_filename=1;;
  62.   --v*)        echo "$version" || exit 2; exit;;
  63.   --wi*)    with_filename=1;;
  64.   --*)    ;;
  65.   -*)
  66.     case "$1" in
  67.     -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
  68.     esac
  69.     case "$1" in
  70.     -*H*)    with_filename=1;;
  71.     esac
  72.     case "$1" in
  73.     -*h*)    no_filename=1;;
  74.     esac
  75.     case "$1" in
  76.     -*L*)    files_without_matches=1;;
  77.     esac
  78.     case "$1" in
  79.     -*l*)    files_with_matches=1;;
  80.     esac;;
  81.   esac
  82.   case "$after_dash_dash$1" in
  83.   -[ef])   opt="$opt $1"; shift; pat="$1"
  84.            if test "$grep" = grep; then  # grep is buggy with -e on SVR4
  85.              grep=egrep
  86.            fi;;
  87.   -[ABCdm])opt="$opt $1 $2"; shift;;
  88.   --)      opt="$opt $1"; after_dash_dash=1;;
  89.   -*)       opt="$opt $1";;
  90.    *)      if test -z "$pat"; then
  91.          pat="$1"
  92.        else
  93.          break;
  94.            fi;;
  95.   esac
  96.   shift
  97. done
  98.  
  99. if test -z "$pat"; then
  100.   echo "$usage"
  101.   exit 2
  102. fi
  103.  
  104. if test $# -eq 0; then
  105.   gzip -cdfq | $grep $opt "$pat"
  106.   exit $?
  107. fi
  108.  
  109. res=0
  110. for i do
  111.   gzip -cdfq -- "$i" |
  112.     if test $files_with_matches -eq 1; then
  113.       $grep $opt "$pat" > /dev/null && printf '%s\n' "$i"
  114.     elif test $files_without_matches -eq 1; then
  115.       $grep $opt "$pat" > /dev/null || printf '%s\n' "$i"
  116.     elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
  117.       $grep $opt "$pat"
  118.     else
  119.       escaped=
  120.       while :; do
  121.     case $i in
  122.     *'
  123. '*)
  124.       char='
  125. '      repl='\\n';;
  126.     *'&'*) char='&' repl='\&';;
  127.     *'\'*) char='\\' repl='\\';;
  128.     *'|'*) char='|' repl='\|';;
  129.     *) break;;
  130.     esac
  131.     up_to_first_char="\\([^$char]*\\)"
  132.     after_first_char="[^$char]*$char\\(.*\\)"
  133.     escaped=$escaped`expr "X$i" : "X$up_to_first_char"`$repl
  134.      i=`expr "X$i" : "$after_first_char"`
  135.       done
  136.       if test $with_filename -eq 1; then
  137.     sed_script="s|[^:]*|$escaped$i|"
  138.       else
  139.     sed_script="s|^|$escaped$i:|"
  140.       fi
  141.  
  142.       # Fail if either grep or sed fails.
  143.       # Bash has ${PIPESTATUS[0]}, but that's not portable.
  144.       exec 3>&1
  145.       r=`
  146.     exec 4>&1
  147.     ($grep $opt "$pat" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
  148.       ` &&
  149.       exit $r
  150.     fi
  151.   r=$?
  152.   test $res -lt $r && res=$r
  153. done
  154. exit $res
  155.